#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h> // 请不要删除本行头文件，否则检查不通过

extern int CurrentCnt; // 请不要删除本行的全局变量声明，否则检查不通过
#define PRINT(x) printf("%d\n",x)
#define DEBUG 0

#if DEBUG

#define GOODS_FILE_NAME "goodsinfo.txt"

#define MAX_ID_LEN 30

#define MAX_NAME_LEN 30

#define MAX_PRICE_LEN 30

#define MAX_DISCOUNT_LEN 30
typedef struct {
char goods_id[MAX_ID_LEN];
char goods_name[MAX_NAME_LEN];
int goods_price;
char goods_discount[MAX_DISCOUNT_LEN];
int goods_amount;
int goods_remain;
} GoodsInfo;


typedef struct node
{
GoodsInfo data;
struct node *next;
} GoodsList;


GoodsInfo read_goods_info();
void init_list(GoodsList **pL);
void destory_list(GoodsList **pL);
void destory_list_and_file(GoodsList **pL);
int save_to_file(GoodsList *L);
void output_one_item(GoodsList *L);
void output_all_items(GoodsList *L);
bool insert_item(GoodsList *L, GoodsInfo item, int choice);
bool delete_item(GoodsList *L, char* goods_id);
GoodsList* search_item(GoodsList *L, char* goods_id);
bool change_item(GoodsList *L, char* goods_id, GoodsInfo new_info);
void bubble_sort(GoodsList *L);
int read_line(char str[], int n);

#else 
#include "lab52.h"

#endif

void init_list(GoodsList **pL)
{
   // PRINT(1);
    *pL = (GoodsList *)malloc(sizeof(GoodsList));
    if(*pL==NULL ) return;
    GoodsList *head = *pL;
    FILE *file = fopen(GOODS_FILE_NAME, "a+");
    GoodsInfo goodinfo;
    if(file==NULL) return;
    int cnt = 0;
  // printf("%d\n",file);
    // fscanf(file, "%s%s%d%s%d%d", goodinfo.goods_id,
    //               goodinfo.goods_name,
    //               goodinfo.goods_price,
    //               goodinfo.goods_discount,
    //               goodinfo.goods_amount,
    //               goodinfo.goods_remain);
    // PRINT(goodinfo.goods_price);
    while (fscanf(file, "%s %s %d %s %d %d\n", goodinfo.goods_id,
                  goodinfo.goods_name,
                  &goodinfo.goods_price,
                  goodinfo.goods_discount,
                  &goodinfo.goods_amount,
                  &goodinfo.goods_remain)!=EOF)
    {
        //PRINT(goodinfo.goods_price);
        GoodsList *tmpGoodList = (GoodsList *)malloc(sizeof(GoodsList));
        if(tmpGoodList == NULL) continue;
        
        tmpGoodList->data = goodinfo;
       // PRINT(tmpGoodList->data.goods_price);
        tmpGoodList -> next = head ->next;
        head ->next = tmpGoodList;
      //  PRINT(head ->next ->data.goods_price);
        cnt++;
    }
    *pL  = head;
    //PRINT(head ->next ->data.goods_price);
    return;
}
void test(GoodsList *gl)
{ 
  GoodsList * now = gl;
  while(now->next!=NULL)
  {
    PRINT(now->next->data.goods_price);
    now = now->next;
  }
  return;
}
int main() 
{
    GoodsList *goodslist;
    GoodsInfo tempGood;
    init_list(&goodslist);
    test(goodslist);
}